<!-- This comment will put IE 6, 7 and 8 in quirks mode -->
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
<title>examples/Supervised/MNISTForExperts.cpp Source File</title>
<script type="text/javaScript" src="search/search.js"></script>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="dynsections.js"></script>
<script src="https://polyfill.io/v3/polyfill.min.js?features=es6"></script>
<script id="MathJax-script" async src="https://cdn.jsdelivr.net/npm/mathjax@3.0.1/es5/tex-mml-chtml.js"></script>
<script src="../../mlstyle.js"></script>
<link href="../css/besser.css" rel="stylesheet" type="text/css"/>
</head>
<!-- pretty cool: each body gets an id tag which is the basename of the web page  -->
<!--              and allows for page-specific CSS. this is client-side scripted, -->
<!--              so the id will not yet show up in the served source code -->
<script type="text/javascript">
    jQuery(document).ready(function () {
        var url = jQuery(location).attr('href');
        var pname = url.substr(url.lastIndexOf("/")+1, url.lastIndexOf(".")-url.lastIndexOf("/")-1);
        jQuery('#this_url').html('<strong>' + pname + '</strong>');
        jQuery('body').attr('id', pname);
    });
</script>
<body>
    <div id="shark_old">
        <div id="wrap">
            <div id="header">
                <div id="site-name"><a href="../../sphinx_pages/build/html/index.html">Shark machine learning library</a></div>
                <ul id="nav">
                    <li >
                        <a href="../../sphinx_pages/build/html/rest_sources/installation.html">Installation</a>
                    </li>
		    <li >
                        <a href="../../sphinx_pages/build/html/rest_sources/tutorials/tutorials.html">Tutorials</a>
                    </li>
		    <li >
                        <a href="../../sphinx_pages/build/html/rest_sources/benchmark.html">Benchmarks</a>
                    </li>
                    <li class="active">
                        <a href="classes.html">Documentation</a>
                        <ul>
                            <li class="first"></li>
                            <li><a href="../../sphinx_pages/build/html/rest_sources/quickref/quickref.html">Quick references</a></li>
                            <li><a href="classes.html">Class list</a></li>
                            <li class="last"><a href="group__shark__globals.html">Global functions</a></li>
                        </ul>
                    </li>
                </ul>
            </div>
        </div>
    </div>
<div id="doxywrapper">
<!--
    <div id="global_doxytitle">Doxygen<br>Documentation:</div>
-->
    <div id="navrow_wrapper">
<!-- Generated by Doxygen 1.9.8 -->
<div id="nav-path" class="navpath">
  <ul>
<li class="navelem"><a class="el" href="dir_d28a4824dc47e487b107a5db32ef43c4.html">examples</a></li><li class="navelem"><a class="el" href="dir_ca5943d51a26be5f1f9bc4d7d5956bc4.html">Supervised</a></li>  </ul>
</div>
</div><!-- top -->
<div class="header">
  <div class="headertitle"><div class="title">MNISTForExperts.cpp</div></div>
</div><!--header-->
<div class="contents">
<a href="_m_n_i_s_t_for_experts_8cpp.html">Go to the documentation of this file.</a><div class="fragment"><div class="line"><a id="l00001" name="l00001"></a><span class="lineno">    1</span><span class="preprocessor">#include &lt;<a class="code" href="_sparse_data_8h.html">shark/Data/SparseData.h</a>&gt;</span><span class="comment">//for reading in the images as sparseData/Libsvm format</span></div>
<div class="line"><a id="l00002" name="l00002"></a><span class="lineno">    2</span><span class="preprocessor">#include &lt;<a class="code" href="_linear_model_8h.html">shark/Models/LinearModel.h</a>&gt;</span><span class="comment">//single dense layer</span></div>
<div class="line"><a id="l00003" name="l00003"></a><span class="lineno">    3</span><span class="preprocessor">#include &lt;<a class="code" href="_convolutional_model_8h.html">shark/Models/ConvolutionalModel.h</a>&gt;</span><span class="comment">//single convolutional layer</span></div>
<div class="line"><a id="l00004" name="l00004"></a><span class="lineno">    4</span><span class="preprocessor">#include &lt;<a class="code" href="_pooling_layer_8h.html">shark/Models/PoolingLayer.h</a>&gt;</span> <span class="comment">//pooling after convolution</span></div>
<div class="line"><a id="l00005" name="l00005"></a><span class="lineno">    5</span><span class="preprocessor">#include &lt;<a class="code" href="_concatenated_model_8h.html">shark/Models/ConcatenatedModel.h</a>&gt;</span><span class="comment">//for stacking layers</span></div>
<div class="line"><a id="l00006" name="l00006"></a><span class="lineno">    6</span><span class="preprocessor">#include &lt;<a class="code" href="_adam_8h.html">shark/Algorithms/GradientDescent/Adam.h</a>&gt;</span><span class="comment">// The Adam optimization algorithm</span></div>
<div class="line"><a id="l00007" name="l00007"></a><span class="lineno">    7</span><span class="preprocessor">#include &lt;<a class="code" href="_cross_entropy_8h.html">shark/ObjectiveFunctions/Loss/CrossEntropy.h</a>&gt;</span> <span class="comment">//classification loss</span></div>
<div class="line"><a id="l00008" name="l00008"></a><span class="lineno">    8</span><span class="preprocessor">#include &lt;<a class="code" href="_error_function_8h.html">shark/ObjectiveFunctions/ErrorFunction.h</a>&gt;</span> <span class="comment">//Error function for optimization</span></div>
<div class="line"><a id="l00009" name="l00009"></a><span class="lineno">    9</span><span class="preprocessor">#include &lt;<a class="code" href="_zero_one_loss_8h.html">shark/ObjectiveFunctions/Loss/ZeroOneLoss.h</a>&gt;</span> <span class="comment">//evaluation for testing</span></div>
<div class="line"><a id="l00010" name="l00010"></a><span class="lineno">   10</span><span class="keyword">using namespace </span><a class="code hl_namespace" href="namespaceshark.html" title="AbstractMultiObjectiveOptimizer.">shark</a>;</div>
<div class="line"><a id="l00011" name="l00011"></a><span class="lineno">   11</span> </div>
<div class="foldopen" id="foldopen00012" data-start="{" data-end="}">
<div class="line"><a id="l00012" name="l00012"></a><span class="lineno"><a class="line" href="_m_n_i_s_t_for_experts_8cpp.html#a3c04138a5bfe5d72780bb7e82a18e627">   12</a></span><span class="keywordtype">int</span> <a class="code hl_function" href="_datasets_8cpp.html#ae66f6b31b5ad750f1fe042a706a4e3d4">main</a>(<span class="keywordtype">int</span> argc, <span class="keywordtype">char</span> **argv)</div>
<div class="line"><a id="l00013" name="l00013"></a><span class="lineno">   13</span>{</div>
<div class="line"><a id="l00014" name="l00014"></a><span class="lineno">   14</span>    <span class="keywordflow">if</span>(argc &lt; 2) {</div>
<div class="line"><a id="l00015" name="l00015"></a><span class="lineno">   15</span>        std::cerr &lt;&lt; <span class="stringliteral">&quot;usage: &quot;</span> &lt;&lt; argv[0] &lt;&lt; <span class="stringliteral">&quot; path/to/mnist_subset.libsvm&quot;</span> &lt;&lt; std::endl;</div>
<div class="line"><a id="l00016" name="l00016"></a><span class="lineno">   16</span>        <span class="keywordflow">return</span> 1;</div>
<div class="line"><a id="l00017" name="l00017"></a><span class="lineno">   17</span>    }</div>
<div class="line"><a id="l00018" name="l00018"></a><span class="lineno">   18</span>    </div>
<div class="line"><a id="l00019" name="l00019"></a><span class="lineno">   19</span>    <span class="comment">//Step1: load data, adapt shapes</span></div>
<div class="line"><a id="l00020" name="l00020"></a><span class="lineno">   20</span>    <a class="code hl_class" href="classshark_1_1_labeled_data.html" title="Data set for supervised learning.">LabeledData&lt;FloatVector,unsigned int&gt;</a> data;</div>
<div class="line"><a id="l00021" name="l00021"></a><span class="lineno">   21</span>    <a class="code hl_function" href="group__shark__globals.html#ga05146914cce29f558409be3d941da4ea" title="Import classification data from a sparse data (libSVM) file.">importSparseData</a>( data, argv[1] , 784 , 100);</div>
<div class="line"><a id="l00022" name="l00022"></a><span class="lineno">   22</span>    std::cout&lt;&lt;<span class="stringliteral">&quot;input shape:&quot;</span>&lt;&lt; data.<a class="code hl_function" href="group__shark__globals.html#ga134d41e34c69c494346367a570bf4ff8" title="Returns the Shape of the inputs.">inputShape</a>()&lt;&lt;std::endl;</div>
<div class="line"><a id="l00023" name="l00023"></a><span class="lineno">   23</span>    std::cout&lt;&lt;<span class="stringliteral">&quot;output shape:&quot;</span>&lt;&lt; data.<a class="code hl_function" href="group__shark__globals.html#ga7f3308a970a6f4fe96aebf23755a6430" title="Returns the Shape of the labels.">labelShape</a>()&lt;&lt;std::endl;</div>
<div class="line"><a id="l00024" name="l00024"></a><span class="lineno">   24</span>    data.<a class="code hl_function" href="group__shark__globals.html#ga134d41e34c69c494346367a570bf4ff8" title="Returns the Shape of the inputs.">inputShape</a>() = {28,28,1}; <span class="comment">//store shape for model creation</span></div>
<div class="line"><a id="l00025" name="l00025"></a><span class="lineno">   25</span>    std::cout&lt;&lt;<span class="stringliteral">&quot;input shape:&quot;</span>&lt;&lt; data.<a class="code hl_function" href="group__shark__globals.html#ga134d41e34c69c494346367a570bf4ff8" title="Returns the Shape of the inputs.">inputShape</a>()&lt;&lt;std::endl;</div>
<div class="line"><a id="l00026" name="l00026"></a><span class="lineno">   26</span> </div>
<div class="line"><a id="l00027" name="l00027"></a><span class="lineno">   27</span>    <span class="comment">//Step 2: define model</span></div>
<div class="line"><a id="l00028" name="l00028"></a><span class="lineno">   28</span>    <a class="code hl_class" href="classshark_1_1_conv2_d_model.html" title="Convolutional Model for 2D image data.">Conv2DModel&lt;FloatVector, RectifierNeuron&gt;</a> conv1(data.<a class="code hl_function" href="group__shark__globals.html#ga134d41e34c69c494346367a570bf4ff8" title="Returns the Shape of the inputs.">inputShape</a>(), {32, 5, 5});</div>
<div class="line"><a id="l00029" name="l00029"></a><span class="lineno">   29</span>    <a class="code hl_class" href="classshark_1_1_pooling_layer.html" title="Performs Pooling operations for a given input image.">PoolingLayer&lt;FloatVector&gt;</a> pooling1(conv1.<a class="code hl_function" href="classshark_1_1_conv2_d_model.html#ac9d63e4f9c8de8c29fe5995555581f4f" title="Returns the shape of the output.">outputShape</a>(), {2, 2}, Pooling::Maximum, Padding::Valid);</div>
<div class="line"><a id="l00030" name="l00030"></a><span class="lineno">   30</span>    <a class="code hl_class" href="classshark_1_1_conv2_d_model.html" title="Convolutional Model for 2D image data.">Conv2DModel&lt;FloatVector, RectifierNeuron&gt;</a> conv2(pooling1.<a class="code hl_function" href="classshark_1_1_pooling_layer.html#ad9fb0c664cd068033bf1de655fde6bd3" title="Returns the shape of the output.">outputShape</a>(), {64, 5, 5});</div>
<div class="line"><a id="l00031" name="l00031"></a><span class="lineno">   31</span>    <a class="code hl_class" href="classshark_1_1_pooling_layer.html" title="Performs Pooling operations for a given input image.">PoolingLayer&lt;FloatVector&gt;</a> pooling2(conv2.<a class="code hl_function" href="classshark_1_1_conv2_d_model.html#ac9d63e4f9c8de8c29fe5995555581f4f" title="Returns the shape of the output.">outputShape</a>(), {2, 2}, Pooling::Maximum, Padding::Valid);</div>
<div class="line"><a id="l00032" name="l00032"></a><span class="lineno">   32</span>    <a class="code hl_class" href="classshark_1_1_linear_model.html" title="Linear Prediction with optional activation function.">LinearModel&lt;FloatVector, RectifierNeuron&gt;</a> dense1(pooling2.<a class="code hl_function" href="classshark_1_1_pooling_layer.html#ad9fb0c664cd068033bf1de655fde6bd3" title="Returns the shape of the output.">outputShape</a>(), 1024, <span class="keyword">true</span>);</div>
<div class="line"><a id="l00033" name="l00033"></a><span class="lineno">   33</span>    <a class="code hl_class" href="classshark_1_1_linear_model.html" title="Linear Prediction with optional activation function.">LinearModel&lt;FloatVector&gt;</a> dense2(dense1.<a class="code hl_function" href="classshark_1_1_linear_model.html#a9eeb86bc2b2c822fa5b9617e80a98d91" title="Returns the shape of the output.">outputShape</a>(), data.<a class="code hl_function" href="group__shark__globals.html#ga7f3308a970a6f4fe96aebf23755a6430" title="Returns the Shape of the labels.">labelShape</a>(), <span class="keyword">true</span>);</div>
<div class="line"><a id="l00034" name="l00034"></a><span class="lineno">   34</span>    <span class="keyword">auto</span> model = conv1 &gt;&gt; pooling1 &gt;&gt; conv2 &gt;&gt; pooling2 &gt;&gt; dense1 &gt;&gt; dense2;</div>
<div class="line"><a id="l00035" name="l00035"></a><span class="lineno">   35</span>    </div>
<div class="line"><a id="l00036" name="l00036"></a><span class="lineno">   36</span>    <a class="code hl_class" href="classshark_1_1_cross_entropy.html" title="Error measure for classification tasks that can be used as the objective function for training.">CrossEntropy&lt;unsigned int, FloatVector&gt;</a> loss;</div>
<div class="line"><a id="l00037" name="l00037"></a><span class="lineno">   37</span>    <a class="code hl_class" href="classshark_1_1_error_function.html" title="Objective function for supervised learning.">ErrorFunction&lt;FloatVector&gt;</a> error(data, &amp;model, &amp;loss, <span class="keyword">true</span>);</div>
<div class="line"><a id="l00038" name="l00038"></a><span class="lineno">   38</span>    </div>
<div class="line"><a id="l00039" name="l00039"></a><span class="lineno">   39</span>    <span class="comment">//Step 4 set up optimizer and run optimization</span></div>
<div class="line"><a id="l00040" name="l00040"></a><span class="lineno">   40</span>    std::size_t iterations = 20001;</div>
<div class="line"><a id="l00041" name="l00041"></a><span class="lineno">   41</span>    <a class="code hl_function" href="group__shark__globals.html#gaa595fd92ec7d8eebcffd070131b18560" title="Initialize model parameters normally distributed.">initRandomNormal</a>(model,0.0001); <span class="comment">//init model</span></div>
<div class="line"><a id="l00042" name="l00042"></a><span class="lineno">   42</span>    <a class="code hl_class" href="classshark_1_1_adam.html" title="Adaptive Moment Estimation Algorithm (ADAM)">Adam&lt;FloatVector&gt;</a> optimizer;</div>
<div class="line"><a id="l00043" name="l00043"></a><span class="lineno">   43</span>    optimizer.<a class="code hl_function" href="classshark_1_1_adam.html#aa8cda0391795c0e586a5dfcef078b15e" title="set learning rate eta">setEta</a>(0.0001);<span class="comment">//learning rate of the algorithm</span></div>
<div class="line"><a id="l00044" name="l00044"></a><span class="lineno">   44</span>    error.<a class="code hl_function" href="classshark_1_1_error_function.html#a6ba22ddebbfc72a20503c9089e59abe8">init</a>();</div>
<div class="line"><a id="l00045" name="l00045"></a><span class="lineno">   45</span>    optimizer.<a class="code hl_function" href="classshark_1_1_adam.html#a05893bc5dc81a6fccd0b9a0a15415770">init</a>(error);</div>
<div class="line"><a id="l00046" name="l00046"></a><span class="lineno">   46</span>    std::cout&lt;&lt;<span class="stringliteral">&quot;Optimizing model &quot;</span>&lt;&lt;std::endl;</div>
<div class="line"><a id="l00047" name="l00047"></a><span class="lineno">   47</span>    <span class="keywordflow">for</span>(std::size_t i = 0; i != iterations; ++i){</div>
<div class="line"><a id="l00048" name="l00048"></a><span class="lineno">   48</span>        optimizer.<a class="code hl_function" href="classshark_1_1_adam.html#aad42982976c3e91534ac33999d7c6fc3" title="Performs a step of the optimization.">step</a>(error);</div>
<div class="line"><a id="l00049" name="l00049"></a><span class="lineno">   49</span>        <span class="keywordflow">if</span>(i  % 100 == 0){<span class="comment">//print out timing information and training error</span></div>
<div class="line"><a id="l00050" name="l00050"></a><span class="lineno">   50</span>            <a class="code hl_class" href="classshark_1_1_zero_one_loss.html" title="0-1-loss for classification.">ZeroOneLoss&lt;unsigned int, FloatVector&gt;</a> classificationLoss;</div>
<div class="line"><a id="l00051" name="l00051"></a><span class="lineno">   51</span>            <span class="keywordtype">double</span> error = classificationLoss(data.<a class="code hl_function" href="group__shark__globals.html#ga6328a5aa2570c01a5ac5f25076071663" title="Access to labels as a separate container.">labels</a>(),model(data.<a class="code hl_function" href="group__shark__globals.html#ga6f74e657c7e0c8a32b2456fb328bd653" title="Access to inputs as a separate container.">inputs</a>()));</div>
<div class="line"><a id="l00052" name="l00052"></a><span class="lineno">   52</span>            std::cout&lt;&lt;i&lt;&lt;<span class="stringliteral">&quot; &quot;</span>&lt;&lt;optimizer.<a class="code hl_function" href="classshark_1_1_abstract_single_objective_optimizer.html#a0909596fcc4f80a8d108859b20b64a81" title="returns the current solution of the optimizer">solution</a>().<a class="code hl_variable" href="structshark_1_1_result_set.html#abfb2c7bc8ee3b184bbef15cb250ead50">value</a>&lt;&lt;<span class="stringliteral">&quot; &quot;</span>&lt;&lt;error&lt;&lt;std::endl;</div>
<div class="line"><a id="l00053" name="l00053"></a><span class="lineno">   53</span>        }</div>
<div class="line"><a id="l00054" name="l00054"></a><span class="lineno">   54</span>    }</div>
<div class="line"><a id="l00055" name="l00055"></a><span class="lineno">   55</span>}</div>
</div>
<div class="line"><a id="l00056" name="l00056"></a><span class="lineno">   56</span> </div>
</div><!-- fragment --></div><!-- contents -->
</div>
</body>
</html>
